# 获取最近一日全国COVID-19情况
import requests
from bs4 import BeautifulSoup
import re
import json
# 1.发送请求 获取疫情首页
response = requests.get('http://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page = response.content.decode()

# 2.提取一日各国疫情数据
soup = BeautifulSoup(home_page, 'lxml')
script = soup.find(id='getListByCountryTypeService2true')
text = script.string

# 3.从数据中获取json格式的字符串
json_str = re.findall(r'\[.+\]', text)[0]

# 4.把json格式字符串转换为python类型
last_day_corona_virus = json.loads(json_str)

# 5.以json格式保存最近1日各国疫情数据
with open('Web_spider/data/last_day_corona_virus.json', 'w',encoding='utf-8') as fp:
    json.dump(last_day_corona_virus, fp, ensure_ascii=False)
